package com.yiren.dbaa.module.common.view.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.model.ScreenModel;
import com.yiren.dbaa.module.common.view.activity.MainActivity;
import com.yiren.dbaa.util.GraphicUtil;
import com.yiren.dbaa.zxing.encoding.ZXingUtils;

/**
 * Describe 风控设备无法使用对话框
 *
 * @author WRX
 * @date 2022/7/7
 */
public class RiskControlDialog {

    /**
     * 显示设备无法使用的对话框
     *
     * @param type 1: 锁机
     *             2：服务器返回无法使用的原因
     *             3: 超过处理器无效设备
     *             4：无网使用免费播报超时
     *             5: 无网使用体验券超时
     *             6: 无网使用测试时间超时
     */
    public static void showDeviceUnuseDialog(Activity mActivity, int type, String content, String url, View.OnClickListener listener) {
        AlertDialog dialog = new AlertDialog.Builder(mActivity).create();
        dialog.setCancelable(false);
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        ScreenModel screen = GraphicUtil.getResolutionInfo(mActivity);
        params.width = (int) ((screen.getWidth() * 5) / 10);
//        params.height = (screen.getHeight() * 8) / 10;
        dialog.getWindow().setAttributes(params);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_device_unuse);
        TextView tvTitle = window.findViewById(R.id.tv_title);
        LinearLayout llQrCode = window.findViewById(R.id.ll_qr_code);
        ImageView ivContent = window.findViewById(R.id.iv_content);
        TextView tvSure = window.findViewById(R.id.tv_sure);
        String txt = mActivity.getString(R.string.device_unuse_title_1);
        if (type == 1) {
            llQrCode.setVisibility(View.GONE);
            tvTitle.setText(content);
        } else if (type == 4 || type == 5 || type == 6) {
            llQrCode.setVisibility(View.GONE);
            txt = txt.replace("##", content);
            tvTitle.setText(txt);
        } else {
            tvTitle.setText(content);
            if (type == 2) {
                llQrCode.setVisibility(View.VISIBLE);
                Bitmap bitmap = ZXingUtils.createQRImage(url, 200, 200);
                Glide.with(mActivity).load(bitmap).into(ivContent);
            } else {
                llQrCode.setVisibility(View.GONE);
            }
        }
        tvSure.setOnClickListener(v -> {
            if (type == 1) {
                mActivity.startActivity(new Intent(mActivity, MainActivity.class));
            } else {
                listener.onClick(v);
            }
            dialog.dismiss();
        });
    }
}
